﻿using CardMonitor.Model;
using CardMonitor.Utils;
using CardMonitor.Windows;
using DotNetSpeech;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace CardMonitor.Pages
{
    /// <summary>
    /// ConfirmPage.xaml 的交互逻辑
    /// </summary>
    public partial class ConfirmPage : Page
    {
        BillModel billModel = null;
        private PortControlHelper pchSend;
        private PortControlHelper portWrite;
        private String curInputTag = "mobile";
        private SolidColorBrush activeBrush = new SolidColorBrush(Color.FromRgb(186, 234, 255));
        MainWindow mainWindow = null;
        SynchronizationContext _syncContext = null;
        ImageBrush imageBrush;
        SpVoice speech = new SpVoice();
        System.Drawing.Image image;
        IntPtr icdev = IntPtr.Zero;
        int st;
        public ConfirmPage()
        {
            InitializeComponent();
            billModel = new BillModel();
            _syncContext = SynchronizationContext.Current;

            this.DataContext = billModel;
            mainWindow = (MainWindow)Window.GetWindow(this);

            this.Loaded += MMPage_Loaded;
        }
        public void MMPage_Loaded(object sender, RoutedEventArgs e)
        {
            if (!"销售业务".Equals(billModel.Type)&&!"采购退货业务".Equals(billModel.Type))
            {
                txtWaiPi.Visibility = Visibility.Visible;
                wpBorder.Visibility = Visibility.Visible;
                txtWaiMao.Visibility = Visibility.Visible;
                wmBorder.Visibility = Visibility.Visible;
            }
            //读取IC卡

        }
        public ConfirmPage(BillModel billModel, MainWindow _win)
        {
            InitializeComponent();
            _syncContext = SynchronizationContext.Current;

            this.billModel = billModel;
            this.Loaded += new RoutedEventHandler(Sign_Loaded);

            this.DataContext = billModel;
            mainWindow = _win;
            this.Loaded += MMPage_Loaded;
        }
        public void setPersonImage(System.Drawing.Image image)
        {

            System.Drawing.Image bmp = image; // 自己初始化的有效的 image
            System.IO.MemoryStream ms = new System.IO.MemoryStream();
            System.Windows.Media.Imaging.BitmapImage bi = new System.Windows.Media.Imaging.BitmapImage();
            bi.BeginInit();
            bi.StreamSource = new MemoryStream(ms.ToArray()); // 不要直接使用 ms
            bi.EndInit();
            cardImage.Source = bi; // done!
            ms.Close();

            //System.Drawing.Image bmp = image; // 自己初始化的有效的 image
            //System.Windows.Media.Imaging.BitmapSource bi =
            //System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(
            //bmp.GetHbitmap(),
            //IntPtr.Zero,
            //Int32Rect.Empty,
            //BitmapSizeOptions.FromEmptyOptions());
            // 上面以 bmp 格式为例的,其他格式自处理
            //this.cardImage.Source = bi; // Done!
            this.image = image;
            //this.cardImage.Source= BitmapFrame.Create(image, BitmapCreateOptions.None, BitmapCacheOption.Default);;
        }
        private void Sign_Loaded(object sender, RoutedEventArgs e)
        {
            //MessageBox.Show(cardInfo.CardNo);
            //System.IO.MemoryStream objMSPhoto = new System.IO.MemoryStream(cardInfo.ArrPhotoByte, 0, cardInfo.ArrPhotoByte.Length);

            //ImageBrush imageBrush = new ImageBrush();
            //ImageSourceConverter imageSourceConverter = new ImageSourceConverter();
            //imageBrush.ImageSource = (ImageSource)imageSourceConverter.ConvertFrom(objMSPhoto);

            //this.cardImagePanel.Background = imageBrush;

            //MessageBox.Show(billModel.CarNo);
        }
        private void goIndex(object sender, RoutedEventArgs e)
        {
            Index index = new Index(mainWindow);
            mainWindow.BreakSpeak();
            CardDeviceUtil.ReceiveCard();
            NavigationService.GetNavigationService(this).Navigate(index);

            //NavigationService.GetNavigationService(this).Navigate(new Uri("/Pages/Index.xaml", UriKind.Relative));
        }

        private void showMessage(String message)
        {
            InfoWindow infoWindow = new InfoWindow(message);
            infoWindow.Owner = mainWindow;
            infoWindow.ShowDialog();
        }
        private void doMakeCard(object sender, RoutedEventArgs e)
        {

            mainWindow.BreakSpeak();

            if ("".Equals(billModel.DriverPhone) || billModel.DriverPhone.Length != 11)
            {
                showMessage("请正确输入手机号码！");
                return;
            }
            //String ifstatement = App.getConfig().IfStatement;
            //if (ifstatement.Equals("0"))
            //{
            //    bool flag = click_statement();
            //    if (!flag)
            //    {
            //        NavigationService.GetNavigationService(this).GoBack();
            //        return;
            //    }
            //}


            HttpPostWindow httpPostWindow = new HttpPostWindow();
            httpPostWindow.Owner = mainWindow;
            JObject paramJSON = new JObject();
            paramJSON.Add("id", billModel.Id);
            paramJSON.Add("facId", billModel.DriverCard);

            httpPostWindow.setParamObject(paramJSON);

            httpPostWindow.setOperation(HttpPostWindow.OperationType.uploadMakeCardResult.ToString());
            bool result = (bool)httpPostWindow.ShowDialog();
            if (!result)
            {
                string message = httpPostWindow.getMessage();
                if (null == message || "".Equals(message))
                {
                    message = "签到失败,请联系管理员";
                }
               // mainWindow.Speak("签到失败，请联系客服人工处理");
                showMessage(message);
                NavigationService.GetNavigationService(this).GoBack();

                return;
            }


           // mainWindow.Speak("车辆信息确认完成");
            String url = "http://" + App.getConfig().HttpServerIp + ":" + App.getConfig().HttpServerPort + "/guns/sysFileInfo/upload2";

            //UploadImage(url, "c:\\azp.bmp",billModel.Id);
            showMessage("车辆信息确认完成");
            //billModel._printCardTime = DateTime.Now.ToString();
            //if (App.getConfig().tipsPrint.Equals("0"))
            //{
            /*
                PrintWindow printWindow = new PrintWindow();
                billModel.CurTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                printWindow.setBillMode(billModel);
                if ("1".Equals(billModel.Type))
                {
                    printWindow.setTemplete("tplSaleIn","1");
                }
                else
                {
                    printWindow.setTemplete("tplBuyIn","1");
                }


                printWindow.Owner = mainWindow;
                printWindow.ShowDialog();
            */

            Index index = new Index(mainWindow);

            NavigationService.GetNavigationService(this).Navigate(index);

        }

        private void MakeCardSucces()
        {
            showMessage("制卡成功,请取卡");
            CardDeviceUtil.PopupCard();
            Reader.hc_exit(icdev);


        }
        private void MakeCardError()
        {
            showMessage("制卡失败,请联系管理员");
        }
    }
}
